#include<bits/stdc++.h>
#define ls(v) ((v)<<1)
#define rs(v) (((v)<<1)|1)
#define ll long long
using namespace std;
const int N=2e5,M=8e5+10;
int aa[N],a[N],c[N],d[M],n,le,m,t,po[N],u;int p[M],now[N],o[M];
int s[N],rr[N];ll sum[N];
int ge(int x,int y){return p[po[x]+y];}
ll su(ll x){return x*(x+1)/2;}
int marge(int x,int y,int k,int p){
	if(!k){
		if(a[x]>=p)return x;
		else return y;
	}else{
		if(a[y]>=p)return y;
		return x;
	}
}void cdq(int l,int r){
	if(l==r){
		s[l]=l;d[l]=0;rr[l]=l;sum[l]=l;
		return;
	}
	int mid=(l+r)>>1;cdq(l,mid),cdq(mid+1,r);d[l]=d[l]+1;
	int rs=s[mid],now=ge(d[l],(r>>d[l]));//cout<<now<<endl;
	s[r]=marge(rs,s[r],now,d[l]);
	if(l!=1)for(int i=mid+1;i<=r;i++)rr[i]=r;
	if(!now&&(a[rs]<d[l]))for(int i=mid+1;i<=r;i++)sum[i]=r;
    if(l!=1)if(!now&&(a[rs]<d[l]))for(int i=l;i<=mid;i++)s[i]=0;
}void work(){
	cdq(1,1<<le);ll ans=0;
    for(int i=1;i<=m;i++){
    	ll p=0;//sum[c[i]]=rr[c[i]];
    	ans^=p=(1LL*i*(su(rr[c[i]])-su(sum[c[i]])+s[c[i]]));
    }printf("%lld\n",ans);
}char e[N];
int main(){
	freopen("arena.in","r",stdin);
	freopen("arena.out","w",stdout);
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)scanf("%d",&aa[i]);
	for(int i=1;i<=m;i++)scanf("%d",&c[i]);
   // int le=0;
	while((1<<le)<n)le++;
	for(int i=1;i<=le+3;i++)po[i]=po[i-1]+(1<<(le-i));
	for(int i=1;(le-i+1);i++){
		scanf("%s",e+1);
		for(int j=1;j<=(1<<(le-i));j++)p[po[i]+j]=e[j]-'0';
	}scanf("%d",&t);
	while(t--){
		int x[4];
		scanf("%d%d%d%d",&x[0],&x[1],&x[2],&x[3]);
		for(int i=1;i<=n;i++)a[i]=aa[i]^x[i%4];
		work();
	}return 0;
}